package Count;

import java.util.Scanner;
import java.util.regex.Pattern;

public class CountApiStructure {

    //统计一个函数含api和含结构（if、else if、else、while、switch之类）行数
    public int CountApiStructure(String str){
        int result =0 ;
        Scanner sc = new Scanner(str);
        String temp;
        boolean isletter;

        while(sc.hasNextLine()){
            temp = sc.nextLine();
            isletter = Pattern.compile("(?i)[a-z]").matcher(temp).find();   //正则表达式匹配判断是否含有字母
            if (isletter){
                if(temp.contains("new")){
                    result++;
                }else if (temp.contains(".")){
                    if(!temp.contains("this.") && !temp.contains("super.") && !temp.contains("out.")){
                        result++;
                    }
                }else if (IsStructure(temp)){
                        result++;
                }else{

                }
            }
        }
        return result;
    }
    // if、else if、else、while、do while、for、switch、try、 catch 、finally
    public boolean IsStructure(String str){
        boolean result = false;
        if (str.contains("if") || str.contains("else if")|| str.contains("else")|| str.contains("while")|| str.contains("do")
                || str.contains("for")|| str.contains("switch")|| str.contains("case")|| str.contains("default")|| str.contains("try")
                || str.contains("catch")|| str.contains("finally")){
            result = true;
        }
        return result;
    }
}
